<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 服商品管理
 *
 * @icon fa fa-circle-o
 */
class Good extends Backend
{

    /**
     * Orders模型对象
     * @var \app\admin\model\Orders
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Orders;

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with('partya,partyb')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 审核
     */
    public function verify($ids)
    {
        $status = $this->request->param('status');
        $message = $this->request->param('message');
        if (!in_array($status, ['allow', 'refuse'])) {
            $this->error('不允许这样操作');
        }
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error('订单不存在');
        }
        $row->verify_status = $status;
        $row->verify_msg = $message;
        if ($status === 'refuse') {
            $arr = ['purposed', 'signed', 'delivered', 'checked'];
            $index = array_search($row->status, $arr, true);
            $row->status = $arr[$index - 1];
        }
        $result = $row->save();
        if (!$result) {
            $this->error('没有改变任何信息');
        }
        $this->success('操作成功');
    }

}
